package com.sunny.oj.controller;

import com.sunny.oj.common.BaseResponse;
import com.sunny.oj.common.DeleteRequest;
import com.sunny.oj.common.ErrorCode;
import com.sunny.oj.common.ResultUtils;
import com.sunny.oj.exception.BusinessException;
import com.sunny.oj.exception.ThrowUtils;
import com.sunny.oj.model.dto.postcomment.PostCommentAddRequest;
import com.sunny.oj.model.entity.PostComment;
import com.sunny.oj.model.entity.User;
import com.sunny.oj.model.vo.PostCommentVO;
import com.sunny.oj.service.PostCommentService;
import com.sunny.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子接口
 */
@RestController
@RequestMapping("/postComment_comment")
@Slf4j
public class PostCommentController {

    @Resource
    private PostCommentService postCommentService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建
     *
     * @param postCommentAddRequest
     * @param request
     * @return
     */

    @PostMapping("/add")
    public BaseResponse<Long> addPostComment(@RequestBody PostCommentAddRequest postCommentAddRequest, HttpServletRequest request) {
        if (postCommentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PostComment postComment = new PostComment();
        BeanUtils.copyProperties(postCommentAddRequest, postComment);
        postCommentService.validPostComment(postComment, true);
        User loginUser = userService.getLoginUser(request);
        postComment.setUserId(loginUser.getId());
        postComment.setPostId(postCommentAddRequest.getPostId());
        boolean result = postCommentService.save(postComment);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newPostCommentId = postComment.getId();
        return ResultUtils.success(newPostCommentId);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<PostCommentVO> getPostCommentVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PostComment postComment = postCommentService.getById(id);
        if (postComment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(postCommentService.getPostCommentVO(postComment, request));
    }


    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePostComment(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        PostComment oldPostComment = postCommentService.getById(id);
        ThrowUtils.throwIf(oldPostComment == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldPostComment.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = postCommentService.removeById(id);
        return ResultUtils.success(b);
    }
}
